package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.zbkj.dal.entity.product.ProductTemplate;
import com.zbkj.dal.entity.product.ProductTemplateParameter;
import com.zbkj.dal.pojo.request.PageParamRequest;
import com.zbkj.dal.pojo.request.ba.ProductTemplateRequest;
import com.zbkj.dal.pojo.response.ba.ProductTemplateResponse;
import com.zbkj.dal.dao.ProductTemplateMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.service.service.ParameterService;
import com.zbkj.service.service.ProductTemplateParameterService;
import com.zbkj.service.service.ProductTemplateService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 模板 服务实现类
 * </p>
 *
 * @author jhui
 * @since 2023-10-31
 */
@Service
public class ProductTemplateServiceImpl extends ServiceImpl<ProductTemplateMapper, ProductTemplate> implements ProductTemplateService {

    @Autowired
    private ProductTemplateParameterService productTemplateParameterService;
    @Autowired
    private ParameterService parameterService;

    @Override
    public PageInfo<ProductTemplate> pageProductTemplates(PageParamRequest pageParamRequest) {
        LambdaQueryWrapper<ProductTemplate> queryWrapper = Wrappers.lambdaQuery(ProductTemplate.class);
        queryWrapper.eq(ProductTemplate::getIsDel, 0);
        List<ProductTemplate> list = list(queryWrapper);
        PageInfo<ProductTemplate> pageInfo = PageInfo.of(list);
        return pageInfo;
    }

    @Override
    @Transactional
    public Boolean saveProductTemplate(ProductTemplateRequest productTemplateRequest) {
        Boolean result = false;
        Integer templateId = productTemplateRequest.getId();
        if (productTemplateRequest.getId() == null) {
            ProductTemplate productTemplate = new ProductTemplate();
            BeanUtils.copyProperties(productTemplateRequest, productTemplate);
            result = save(productTemplate);
            templateId = productTemplate.getId();
        } else {
            LambdaUpdateWrapper<ProductTemplate> updateWrapper = Wrappers.lambdaUpdate(ProductTemplate.class);
            updateWrapper.set(ProductTemplate::getName, productTemplateRequest.getName())
                    .set(ProductTemplate::getUpdateTime, LocalDateTime.now())
                    .eq(ProductTemplate::getId, productTemplateRequest.getId());
            result = update(updateWrapper);
            productTemplateParameterService.deleteByTemplateId(productTemplateRequest.getId());
        }

        if (result) {
            if (CollectionUtils.isNotEmpty(productTemplateRequest.getParameterIds())) {
                List<ProductTemplateParameter> productTemplateParameters = Lists.newArrayList();
                for (Integer parameterId : productTemplateRequest.getParameterIds()) {
                    ProductTemplateParameter productTemplateParameter = new ProductTemplateParameter();
                    productTemplateParameter.setProductTemplateId(templateId);
                    productTemplateParameter.setParameterId(parameterId);
                    productTemplateParameters.add(productTemplateParameter);
                }
                productTemplateParameterService.saveBatch(productTemplateParameters);
            }

        }
        return result;
    }

    @Override
    public ProductTemplateResponse getProductTemplate(Integer id) {
        ProductTemplate productTemplate = this.getById(id);
        ProductTemplateResponse productTemplateResponse = new ProductTemplateResponse();
        BeanUtils.copyProperties(productTemplate, productTemplateResponse);

        List<ProductTemplateParameter> productTemplateParameters = productTemplateParameterService.listByTemplateId(productTemplate.getId());
        if (CollectionUtils.isNotEmpty(productTemplateParameters)) {
            List<Integer> parameterIds = productTemplateParameters.stream().map(m -> m.getParameterId()).distinct().collect(Collectors.toList());
            //List<Parameter> parameters = parameterService.listByIds(parameterIds);
            productTemplateResponse.setParameterIds(parameterIds);
        }

        return productTemplateResponse;
    }

    @Override
    public Boolean logicDeleteProductTemplate(Integer id) {
        LambdaUpdateWrapper<ProductTemplate> updateWrapper = Wrappers.lambdaUpdate(ProductTemplate.class);
        updateWrapper.set(ProductTemplate::getIsDel, 1)
                .set(ProductTemplate::getUpdateTime, LocalDateTime.now())
                .eq(ProductTemplate::getId, id);
        return update(updateWrapper);
    }

    @Override
    public List<ProductTemplate> listProductTemplates() {
        LambdaQueryWrapper<ProductTemplate> queryWrapper = Wrappers.lambdaQuery(ProductTemplate.class);
        queryWrapper.eq(ProductTemplate::getIsDel, 0);
        List<ProductTemplate> list = list(queryWrapper);
        return list;
    }
}
